<?php

	class Filters_model extends CI_Model 
	{
		public function __construct()
		{
			parent::__construct();
			 
			// Load database
			$this->load->database();
                        
                        // Load session
                        $this->load->library('session');
		}
		
		// Add filter
		public function add($title, $interval, $provider_data)
		{
                    // Get current user
                    $user = unserialize($this->session->userdata('profile'));
                    
                    $values = array(
				'clientID' => $user->ID,
				'filterData' => json_encode( $provider_data ),
				'interval' => $interval,
                                'title' => $title,
                                'lastFetched' => date('Y-m-d H:i:s', time() - $interval)
                    );
                    
                    $this->db->insert('clientFilter', $values); 
                    return $this->db->insert_id();
		}


		// Return single user record as an object
		public function get($ID, $active = 1)
		{
                    $query = $this->db->get_where('clientFilter', array('ID' => $ID, 'active' => $active));
			
                    if($query->num_rows() > 0)
                    {
                            return $query->row();
                    }
                    else
                    {
                            return NULL;
                    }
		}
                
                // Return all records by user id;
		public function get_by_user_id($ID, $active = 1, $order = "desc")
		{
                    $this->db->order_by("ID", $order); 
                    $query = $this->db->get_where('clientFilter', array('clientID' => $ID, 'active' => $active));
			
                    if($query->num_rows() > 0)
                    {
                            return $query->result();
                    }
                    else
                    {
                            return NULL;
                    }
		}
                
                // Return all records;
		public function get_all($active = 1, $order = "desc")
		{
                    $this->db->order_by("ID", $order); 
                    $query = $this->db->get_where('clientFilter', array('active' => $active));
			
                    if($query->num_rows() > 0)
                    {
                            return $query->result();
                    }
                    else
                    {
                            return NULL;
                    }
		}
		
		
		// Update filter
		public function update($object)
		{
                    if($object->ID)
                    {
                            $this->db->where('ID', $object->ID);
                            $this->db->update('clientFilter', $object); 
                    }
		}
                
                // Delete filter
		public function delete($object)
		{
                    return $this->db->delete('clientFilter', array('ID' => $object->ID)); 
		}
		
	}


?>
